<?php
/**
 * 卖家实物订单管理
 *
 *
 *
 ***/


defined('IN_NWIDC') or exit('Access Invalid!');
class store_orderControl extends BaseSellerControl {
    private $useIETAPI = 0;
    public function __construct() {
        parent::__construct();
        Language::read('member_store_index');
    }

	/*
	 *检查是否拥有导出详细订单的权限
	 *
	 * */
	private function checkAuth(){
		$sinfo = ml('store')->find($_SESSION['store_id']);
		if($sinfo['useIETAPI'] == 1){ $this->useIETAPI = 1;}
		if($this->useIETAPI === 0){
			try {
				$error = '您没有相关权限!';
				throw new Exception($error);
			} catch (Exception $e) {
				header("Content-type:text/html;charset=utf-8");
				echo"<script>alert('".$e->getMessage()."');history.go(-1);</script>";
				exit;
			}
		}
	}

	/**
	 * 订单列表
	 *
	 */
	public function indexOp() {
        $model_order = Model('order');
        $condition = array();
        $condition['store_id'] = $_SESSION['store_id'];
        if ($_GET['order_sn'] != '') {
            $condition['order_sn'] = $_GET['order_sn'];
        }
        if ($_GET['buyer_name'] != '') {
            $condition['buyer_name'] = $_GET['buyer_name'];
        }
        $allow_state_array = array('state_new','state_pay','state_send','state_success','state_cancel','imported_order');
        if (in_array($_GET['state_type'],$allow_state_array)) {
            $condition['order_state'] = str_replace($allow_state_array,
                    array(ORDER_STATE_NEW,ORDER_STATE_PAY,ORDER_STATE_SEND,ORDER_STATE_SUCCESS,ORDER_STATE_CANCEL), $_GET['state_type']);
        } else {
            $_GET['state_type'] = 'store_order';
        }
        $if_start_date = preg_match('/^20\d{2}-\d{2}-\d{2}$/',$_GET['query_start_date']);
        $if_end_date = preg_match('/^20\d{2}-\d{2}-\d{2}$/',$_GET['query_end_date']);
        $start_unixtime = $if_start_date ? strtotime($_GET['query_start_date']) : null;
        $end_unixtime = $if_end_date ? strtotime($_GET['query_end_date']): null;
        if ($start_unixtime || $end_unixtime) {
            $condition['add_time'] = array('time',array($start_unixtime,$end_unixtime));
        }

        if(isset($_GET['is_we_groupbuy'])){
            $condition['is_we_groupbuy'] = $_GET['is_we_groupbuy'];
        }
		if($_GET['order_type']==1){
			$condition['out_order_sn'] = array('exp',"out_order_sn is null");
		}else if($_GET['order_type']==2){
			$condition['out_order_sn'] = array('neq','');
		}else if($_GET['order_type']==3){
			$condition["order_state"] = ORDER_STATE_PAY;
			$condition['is_tax_order'] = 1;
 			$condition['payment_code'] = array('in',array("wxpay", "joinpay1", "joinpay2", "joinpay3", "joinpay4"));
		}else if($_GET['order_type']==4){
			$condition['order_state'] = array('neq',ORDER_STATE_CANCEL);
		}else if($_GET['order_type']==5){
			$condition['order_state'] = array('eq',ORDER_STATE_CANCEL);
		}else if($_GET['order_type']==6){
			$condition['is_tax_order'] = 1;
		}else if($_GET['order_type']==7){
			$condition['order_state'] = ORDER_STATE_PAY;
			$condition['is_tax_order'] = 1;
			$condition['is_pull_kjy'] = 0;
		}

		if($_GET['state_type'] == 'imported_order'){
			if(empty($start_unixtime) && empty($end_unixtime)){
				$condition['add_time'] = array('time',array(date("Ymd"),date("Ymd") + 24 * 3600));
			}
			$condition['out_order_sn'] = array('neq','');
			if(empty($_GET['super_order_state']) || $_GET['super_order_state'] == -1){
				unset($condition['order_state']);
			}
			else{
				$condition['order_state'] = $_GET['super_order_state'];
			}
			$order_list = $model_order->getOrderList($condition, 5000, '*', 'order_id desc','', array('order_goods','order_common','member'));

		}
		else{
			$order_list = $model_order->getOrderList($condition, 20, '*', 'order_id desc','', array('order_goods','order_common','member'));

		}


        //页面中显示那些操作
        foreach ($order_list as $key => $order_info) {

        	//显示取消订单
        	$order_info['if_cancel'] = $model_order->getOrderOperateState('store_cancel',$order_info);

        	//显示调整费用
        	$order_info['if_modify_price'] = $model_order->getOrderOperateState('modify_price',$order_info);

        	//显示发货
        	$order_info['if_send'] = $model_order->getOrderOperateState('send',$order_info);

        	//显示锁定中
        	$order_info['if_lock'] = $model_order->getOrderOperateState('lock',$order_info);

        	//显示物流跟踪
        	$order_info['if_deliver'] = $model_order->getOrderOperateState('deliver',$order_info);
			if(!$order_info['extend_order_goods']){
				$order_info['extend_order_goods'] = [];
			}
        	foreach ($order_info['extend_order_goods'] as $kk => $value) {
        	    $value['image_60_url'] = thumb($value,60);
        	    $value['image_240_url'] = thumb($value, 240);
		    $img = $value['goods_image'];
        	    $value['image_60_url'] = cthumb($value['goods_image'], 60, $value['store_id']);
        	    $value['image_240_url'] = cthumb($value['goods_image'], 240, $value['store_id']);
		    $ginfo = ml('goods')->find($value['goods_id']);
		    if(strpos($value['image_60_url'],'default') !== false){
		    	$value['image_60_url'] = $value['image_240_url'] = '/data/upload/shop/store/goods/' . $ginfo['store_id'] . '/' . $img;
		    }
        	    $value['goods_type_cn'] = orderGoodsType($value['goods_type']);
        	    $value['goods_url'] = urlShop('goods','index',array('goods_id'=>$value['goods_id']));
        	    if ($value['goods_type'] == 5) {
        	        $order_info['zengpin_list'][] = $value;
        	    } else {
        	        $order_info['goods_list'][] = $value;
        	    }

        	}

        	if (empty($order_info['zengpin_list'])) {
        	    $order_info['goods_count'] = count($order_info['goods_list']);
        	} else {
        	    $order_info['goods_count'] = count($order_info['goods_list']) + 1;
        	}
        	$order_list[$key] = $order_info;

        }

	Tpl::output('store_info',$this->store_info);
        Tpl::output('order_list',$order_list);
        Tpl::output('show_page',$model_order->showpage());
        self::profile_menu('list',$_GET['state_type']);

        Tpl::showpage('store_order.index');
	}

	function imported_order_listOp(){
		self::profile_menu('list','imported_order_list');
		Tpl::showpage('store_order.import_list');
	}

	function tonglian_listOp(){
		self::profile_menu('list','tonglian_list');
		Tpl::showpage("store_order.tonglian_list");
	}
	/**
	 * 订单详细列表
	 *
	 */
	public function index_detailOp() {
		$this->checkAuth();
		$model_order = Model('order');
		$condition = array();
		$condition['store_id'] = $_SESSION['store_id'];
		if ($_GET['order_sn'] != '') {
			$condition['order_sn'] = $_GET['order_sn'];
		}
		if ($_GET['buyer_name'] != '') {
			$condition['buyer_name'] = $_GET['buyer_name'];
		}

		$if_start_date = preg_match('/^20\d{2}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}$/',$_GET['query_start_date']);
		$if_end_date = preg_match('/^20\d{2}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}$/',$_GET['query_end_date']);

		$start_unixtime = $if_start_date ? strtotime($_GET['query_start_date']) : null;
		$end_unixtime = $if_end_date ? strtotime($_GET['query_end_date']): null;
		if ($start_unixtime || $end_unixtime) {
			$condition['add_time'] = array('time',array($start_unixtime,$end_unixtime));
		}

		if(empty($_GET['super_order_state']) || $_GET['super_order_state'] == -1){
			unset($condition['order_state']);
		}
		else{
			$condition['order_state'] = $_GET['super_order_state'];
		}

		if(isset($_GET['is_we_groupbuy'])){
			$condition['is_we_groupbuy'] = $_GET['is_we_groupbuy'];
		}

		if($_GET['export']==='y'){
			$order_list = $model_order->getOrderList($condition, 200, '*', 'order_id desc','', array('order_goods','order_common','member'));
			$data['start_time'] = $_GET['query_start_date'];
			$data['end_time'] = $_GET['query_end_date'];
			$data['order_list'] = $order_list;
			$this->orderDetailExportWithoutFileP($data);
			exit;
		}
		$order_list = $model_order->getOrderList($condition, 20, '*', 'order_id desc','', array('order_goods','order_common','member'));
		//页面中显示那些操作
		foreach ($order_list as $key => $order_info) {

			//显示取消订单
			$order_info['if_cancel'] = $model_order->getOrderOperateState('store_cancel',$order_info);

			//显示调整费用
			$order_info['if_modify_price'] = $model_order->getOrderOperateState('modify_price',$order_info);

			//显示发货
			$order_info['if_send'] = $model_order->getOrderOperateState('send',$order_info);

			//显示锁定中
			$order_info['if_lock'] = $model_order->getOrderOperateState('lock',$order_info);

			//显示物流跟踪
			$order_info['if_deliver'] = $model_order->getOrderOperateState('deliver',$order_info);
			if(!$order_info['extend_order_goods']){
				$order_info['extend_order_goods'] = [];
			}

			//order_common信息
			$ocominfo = ml('order_common')->find("order_id = '$order_info[order_id]'");
			$order_info['ocominfo'] = $ocominfo;
			//发票信息
			$inv_info = unserialize($ocominfo['invoice_info']);
			$order_info['inv_info'] = $inv_info;
			//header("Content-type:text/html;charset=utf-8");

			foreach ($order_info['extend_order_goods'] as $kkk => $value) {
				$goods_info = Model()->table('goods')->where('goods_id = '.$value['goods_id'])->find();
				$value['image_60_url'] = cthumb($value['goods_image'], 60, $value['store_id']);
				$value['image_240_url'] = cthumb($value['goods_image'], 240, $value['store_id']);
				$value['goods_type_cn'] = orderGoodsType($value['goods_type']);
				$value['goods_url'] = urlShop('goods','index',array('goods_id'=>$value['goods_id']));
				if ($value['goods_type'] == 5) {
					$order_info['zengpin_list'][] = $value;
				} else {
					$order_info['goods_list'][] = $value;
					$order_info['goods_list'][$kkk]['gdetailinfo'] = $goods_info;
				}
			}

			if (empty($order_info['zengpin_list'])) {
				$order_info['goods_count'] = count($order_info['goods_list']);
			} else {
				$order_info['goods_count'] = count($order_info['goods_list']) + 1;
			}
			$order_list[$key] = $order_info;
		}

		Tpl::output('order_list',$order_list);
		Tpl::output('show_page',$model_order->showpage());
		self::profile_menu('list',$_GET['state_type']);

		Tpl::showpage('store_order.index_detail');
	}


	public function orderDetailExportWithoutFileP($data_list){
		if(is_array($data_list['order_list'])){
			foreach($data_list['order_list'] as $v){
				$sn_arr[] = $v['order_sn'];
			}
		}else{
			$sn_arr = $data_list['order_list'];
		}
		$failed = [];
		$success = [];
		$arr = [
				[],
				['序号','下单时间','订单号','用户名','用户ID','收货人姓名','收货人电话','收货人地址','支付时间','支付方式','支付单号','订单状态','发货时间','送货方式','快递单号','商家名称','商家ID','总金额','运费','税费','预存款','代金券','实际支付金额','佣金比例','佣金金额','签收时间','是否开票','发票抬头','备注','商品货号','商品名称','条形码','商品单价','商品数量']
		];

		foreach($sn_arr as $key => $sn){
			$sn = trim($sn);
			if(empty($sn)){
				continue;
			}
			$oinfo = ml('order')->find("order_sn = '$sn' and store_id = '$_SESSION[store_id]'");
			$ocominfo = ml('order_common')->find("order_id = '$oinfo[order_id]'");
			$ologinfo = ml('order_log')->find("order_id = '$oinfo[order_id]' and log_orderstate = '20'");

			$c = $ologinfo['log_msg'];//支付流水号
			preg_match('/[1-9a-zA-Z]+/',$c,$d);
			$ologserialnum = $d[0]?$d[0]:'无';
			if(!$oinfo){
				$failed[] = [
						$sn,
						"订单不存在"
				];
				continue;
			}
			$oginfo = ml('order_goods')->select('order_id = '.$oinfo['order_id']);
			if(count($oginfo)==0){
				continue;
			}

			//发票信息
			$inv_info = unserialize($ocominfo['invoice_info']);

			switch($oinfo['order_state']){
				case'0'  : $order_state= '已取消';break;
				case'10' : $order_state= '未付款';break;
				case'20' : $order_state= '已付款';break;
				case'30' : $order_state= '已发货';break;
				case'40' : $order_state= '已收货';break;
			}
			$express = ml('express')->find([
					'id' => $ocominfo['shipping_express_id']
			]);

			foreach($oginfo as $k => $ginfo){
				$goods_info = Model()->table('goods')->where('goods_id = '.$ginfo['goods_id'])->find();
				if($k==0){
					$arr[] = [
							++$key,
							date("Y-m-d H:m:s",$oinfo['add_time']),
							$oinfo['order_sn'].' ',
							$oinfo['buyer_name'],
							$oinfo['buyer_id'],
							$oinfo['receiver_name'],
							$oinfo['phone'],
							$oinfo['address'],
							date("Y-m-d H:m:s",$oinfo['payment_time']),
							$oinfo['payment_code'],
							$ologserialnum.' ',
							$order_state,
							date("Y-m-d H:m:s",$ocominfo['shipping_time']),//发货时间
							$express['e_name'],//送货方式
							$oinfo['shipping_code'].' ',
							$oinfo['store_name'],
							$oinfo['store_id'],
							$oinfo['order_amount'],//
							$oinfo['shipping_fee'],
							$oinfo['order_tax'],
							$oinfo['pd_amount'],
							$oinfo['rcb_amount'],
							$oinfo['order_amount']-$oinfo['pd_amount']-$oinfo['rcb_amount'],
							$oinfo['distribute_rate'],
							$oinfo['distribute_price'],
							$oinfo['finnshed_time'],
							$inv_info['抬头']?'是':'否',
							$inv_info['抬头'],
							$ocominfo['deliver_explain'],
							$goods_info['goods_serial'],
							$ginfo['goods_name'],
							$goods_info['sharp'].' ',
							$ginfo['goods_price'],
							$ginfo['goods_num'],
					];
				}else{
					$arr[] = [
							'',
							'',
							'',
							'',
							'',
							'',
							'',
							'',
							'',
							'',
							'',
							'',
							'',//发货时间
							'',//送货方式
							'',
							'',
							'',
							'',
							'',
							'',
							'',
							'',
							'',
							'',
							'',
							'',
							'',
							'',
							'',
							$goods_info['goods_serial'],
							$ginfo['goods_name'],
							$goods_info['sharp'].' ',
							$ginfo['goods_price'],
							$ginfo['goods_num'],
					];
//						$arr[] = [
//								'',
//								'',
//								'订单类型','订单号','用户名','用户ID','收货人姓名','收货人电话','收货人身份证','收货人地址','订单渠道','支付时间','支付方式','支付单号','订单状态','发货时间','送货方式','快递单号','商家名称','商家ID','商品货号','商品名称','条形码','总金额','商品单价','商品数量','运费','税费','预存款','代金券','实际支付金额','佣金比例','佣金金额','签收时间','是否开票','发票抬头','备注'
//						];
				}

			}
			$success[] = [
					$oinfo['order_sn']
			];
		}
		$arr_length = count($arr);
		if($arr_length<3){ header("Content-type:text/html;charset=utf-8"); echo "<script>alert('没有符合的数据!');window.close();</script>"; exit;}
		require_once SIN_ROOT_PATH . '/static/lib/PHPExcel.php';
		require_once SIN_ROOT_PATH . '/static/lib/PHPExcel/IOFactory.php';
		$index = array(
				'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK'
		);
		$objPHPExcel = new PHPExcel();                     //实例化一个PHPExcel()对象
		$objSheet = $objPHPExcel->getActiveSheet();        //选取当前的sheet对象
		$objSheet->setTitle('Simple');
		ini_set("memory_limit", "5120M");
		foreach($arr as $k => $v){
			foreach($v as $kk => $vv){
				$objSheet->getColumnDimension($index[$kk])->setAutoSize(true);
				$objSheet->setCellValue($index[$kk] . $k, $vv);
			}
		}
		$objSheet->setCellValue('A'.($arr_length+2),'下载时间');
		$objSheet->setCellValue('A'.($arr_length+3),date("Y-m-d",time()));
		$objSheet->setCellValue('B'.($arr_length+2),'起始日期');
		$objSheet->setCellValue('B'.($arr_length+3),$data_list['start_time']);
		$objSheet->setCellValue('C'.($arr_length+2),'起始日期');
		$objSheet->setCellValue('C'.($arr_length+3),$data_list['end_time']);
		$objSheet->setCellValue('D'.($arr_length+2),'总交易单数');
		$objSheet->setCellValue('D'.($arr_length+3),count($sn_arr));
		$objSheet->setCellValue('E'.($arr_length+2),'总交易额');
		$objSheet->setCellValue('E'.($arr_length+3),'=SUM(R1:R'.($arr_length-1).')');
		$objSheet->setCellValue('F'.($arr_length+2),'总运费');
		$objSheet->setCellValue('F'.($arr_length+3),'=SUM(S1:S'.($arr_length-1).')');
		$objSheet->setCellValue('G'.($arr_length+2),'总税费');
		$objSheet->setCellValue('G'.($arr_length+3),'=SUM(T1:T'.($arr_length-1).')');
		$objSheet->setCellValue('H'.($arr_length+2),'总实际支付金额');
		$objSheet->setCellValue('H'.($arr_length+3),'=SUM(W1:W'.($arr_length-1).')');
// Redirect output to a client’s web browser (Excel2007)
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Content-Disposition: attachment;filename="excel"'.uniqid().'".xlsx"');
		header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
		header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
		header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
		header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
		header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
		header ('Pragma: public'); // HTTP/1.0

		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
		$objWriter->save('php://output');
	}


	/**
	 * 卖家订单详情
	 *
	 */
	public function show_orderOp() {
		Language::read('member_member_index');
	    $order_id = intval($_GET['order_id']);
	    if ($order_id <= 0) {
	        showMessage(Language::get('wrong_argument'),'','html','error');
	    }
	    $model_order = Model('order');
	    $condition = array();
        $condition['order_id'] = $order_id;
        $condition['store_id'] = $_SESSION['store_id'];
	    $order_info = $model_order->getOrderInfo($condition,array('order_common','order_goods','member'));
	    if (empty($order_info)) {
	        showMessage(Language::get('store_order_none_exist'),'','html','error');
	    }

        $model_refund_return = Model('refund_return');
        $order_list = array();
        $order_list[$order_id] = $order_info;
        $order_list = $model_refund_return->getGoodsRefundList($order_list,1);//订单商品的退款退货显示
        $order_info = $order_list[$order_id];
        $refund_all = $order_info['refund_list'][0];
        if (!empty($refund_all) && $refund_all['seller_state'] < 3) {//订单全部退款商家审核状态:1为待审核,2为同意,3为不同意
            Tpl::output('refund_all',$refund_all);
        }

        //显示锁定中
        $order_info['if_lock'] = $model_order->getOrderOperateState('lock',$order_info);

    	//显示调整费用
    	$order_info['if_modify_price'] = $model_order->getOrderOperateState('modify_price',$order_info);

        //显示取消订单
        $order_info['if_cancel'] = $model_order->getOrderOperateState('buyer_cancel',$order_info);

    	//显示发货
    	$order_info['if_send'] = $model_order->getOrderOperateState('send',$order_info);

        //显示物流跟踪
        $order_info['if_deliver'] = $model_order->getOrderOperateState('deliver',$order_info);

        //显示系统自动取消订单日期
        if ($order_info['order_state'] == ORDER_STATE_NEW) {
            $order_info['order_cancel_day'] = $order_info['add_time'] + ORDER_AUTO_CANCEL_DAY * 24 * 3600;
        }

        //显示快递信息
        if ($order_info['shipping_code'] != '') {
            $express = rkcache('express',true);
            $order_info['express_info']['e_code'] = $express[$order_info['extend_order_common']['shipping_express_id']]['e_code'];
            $order_info['express_info']['e_name'] = $express[$order_info['extend_order_common']['shipping_express_id']]['e_name'];
            $order_info['express_info']['e_url'] = $express[$order_info['extend_order_common']['shipping_express_id']]['e_url'];
        }

        //显示系统自动收获时间
        if ($order_info['order_state'] == ORDER_STATE_SEND) {
            $order_info['order_confirm_day'] = $order_info['delay_time'] + ORDER_AUTO_RECEIVE_DAY * 24 * 3600;
        }

        //如果订单已取消，取得取消原因、时间，操作人
        if ($order_info['order_state'] == ORDER_STATE_CANCEL) {
            $order_info['close_info'] = $model_order->getOrderLogInfo(array('order_id'=>$order_info['order_id']),'log_id desc');
        }

        foreach ($order_info['extend_order_goods'] as $value) {
		$ginfo = ml("goods")->find($value['goods_id']);
            $value['image_60_url'] = cthumb($value['goods_image'], 60, $ginfo['store_id']);
            $value['image_240_url'] = cthumb($value['goods_image'], 240, $ginfo['store_id']);
            $value['goods_type_cn'] = orderGoodsType($value['goods_type']);
            $value['goods_url'] = urlShop('goods','index',array('goods_id'=>$value['goods_id']));
            if ($value['goods_type'] == 5) {
                $order_info['zengpin_list'][] = $value;
            } else {
                $order_info['goods_list'][] = $value;
            }
        }

        if (empty($order_info['zengpin_list'])) {
            $order_info['goods_count'] = count($order_info['goods_list']);
        } else {
            $order_info['goods_count'] = count($order_info['goods_list']) + 1;
        }

        if (empty($order_info['extend_order_common']['reciver_info']['phone'])) {
            $order_info['extend_order_common']['reciver_info']['phone'] = $order_info['extend_order_common']['reciver_info']['mob_phone'];
        }
        if(!empty($order_info['extend_order_common']['reciver_info']['area_info'] ) && strpos($order_info['extend_order_common']['reciver_info']['address'], $order_info['extend_order_common']['reciver_info']['area_info']) === false){
            $order_info['extend_order_common']['reciver_info']['address'] = $order_info['extend_order_common']['reciver_info']['area_info'] . ' '  . $order_info['extend_order_common']['reciver_info']['address'] ;
        }

	    Tpl::output('order_info',$order_info);

        //发货信息
        if (!empty($order_info['extend_order_common']['daddress_id'])) {
            $daddress_info = Model('daddress')->getAddressInfo(array('address_id'=>$order_info['extend_order_common']['daddress_id']));
            Tpl::output('daddress_info',$daddress_info);
        }

		Tpl::showpage('store_order.show');
	}

	/**
	 * 卖家订单状态操作
	 *
	 */
	public function change_stateOp() {
		$state_type	= $_GET['state_type'];
		$order_id	= intval($_GET['order_id']);

		$model_order = Model('order');
		$condition = array();
		$condition['order_id'] = $order_id;
		$condition['store_id'] = $_SESSION['store_id'];
		$order_info	= $model_order->getOrderInfo($condition);

		if ($_GET['state_type'] == 'order_cancel') {
		    $result = $this->_order_cancel($order_info,$_POST);
		} elseif ($_GET['state_type'] == 'modify_price') {
		    $result = $this->_order_ship_price($order_info,$_POST);
		}
        if (!$result['state']) {
            showDialog($result['msg'],'','error',empty($_GET['inajax']) ?'':'CUR_DIALOG.close();');
        } else {
            showDialog($result['msg'],'reload','succ',empty($_GET['inajax']) ?'':'CUR_DIALOG.close();');
        }
	}

	/**
	 * 取消订单
	 * @param unknown $order_info
	 */
	private function _order_cancel($order_info, $post) {
	    $model_order = Model('order');
	    $logic_order = Logic('order');

	    if(!chksubmit()) {
            Tpl::output('order_info',$order_info);
            Tpl::output('order_id',$order_info['order_id']);
            Tpl::showpage('store_order.cancel','null_layout');
            exit();
	     } else {
	         $if_allow = $model_order->getOrderOperateState('store_cancel',$order_info);
	         if (!$if_allow) {
	             return callback(false,'无权操作');
	         }
	         $msg = $post['state_info1'] != '' ? $post['state_info1'] : $post['state_info'];
	         return $logic_order->changeOrderStateCancel($order_info,'seller',$_SESSION['member_name'], $msg);
	     }
	}

	/**
	 * 修改运费
	 * @param unknown $order_info
	 */
	private function _order_ship_price($order_info, $post) {
	    $model_order = Model('order');
	    $logic_order = Logic('order');
	    if(!chksubmit()) {
	        Tpl::output('order_info',$order_info);
	        Tpl::output('order_id',$order_info['order_id']);
            Tpl::showpage('store_order.edit_price','null_layout');
            exit();
        } else {
            $if_allow = $model_order->getOrderOperateState('modify_price',$order_info);
            if (!$if_allow) {
                return callback(false,'无权操作');
            }
            return $logic_order->changeOrderShipPrice($order_info,'seller',$_SESSION['member_name'],$post['shipping_fee']);
        }

	}

	public function export_excelOp(){
		set_time_limit(0);
		$list = [];
		do{
			if(empty($_POST['start_time'])){return;}
			if(empty($_POST['end_time'])){return;}
			$start_time = strtotime($_POST['start_time']);
			$end_time = strtotime($_POST['end_time']);

			$buyer_name = trim($_POST['buyer_name']);
			$order_sn = trim($_POST['order_sn']);

			$sql = "select * from ra_order where order_state >= 20 and add_time between {$start_time} and {$end_time} and out_order_sn is null and import_sn is null and is_tax_order = 1";
			if(!empty($buyer_name)){
				$sql .= " and (buyer_name = '{$buyer_name}' or receiver_name = '{$buyer_name}')";
			}
			if(!empty($order_sn)){
				$sql .= " and order_sn = '{$order_sn}'";
			}
			$list = Model()->query($sql);
		}while(0);

		$arr = [
			[],
			["客户名称","手机号","身份证号","地区名称","街道地址","商品名称","商品货号","单价","计量单位","原产国","商品编码","海关规格型号","品牌","订单总金额","数量","法定第一数量","法定第二数量","快递单号","税费","行政区划代码","商品检验检验备案号","电商企业名称","电商企业代码","实际支付金额","订单编号","支付流水号"]
		];
		foreach($list as $item){

			$ogs = ml('order_goods')->select([
				'order_id' => $item['order_id']
			]);
			$om = ml('order_common')->find([
				'order_id' => $item['order_id']
			]);
			$sinfo = ml('store')->find($item['store_id']);
			if(!$sinfo){
				continue;
			}
			if(!$om){
				continue;
			}
			if(!count($ogs)){
				continue;
			}
			$rinfo = unserialize($om['receiver_info']);
			$p = ml('area')->find($om['reciver_province_id']);
			$c = ml('area')->find($om['reciver_city_id']);
			$a= ml('area')->find($om['reciver_area_id']);

			foreach($ogs as $g){
				$ginfo = ml('goods')->find($g['goods_id']);
				$gminfo = ml('goods_common')->find($ginfo['goods_commonid']);
				$gext = ml('custom_shenzhen_goods')->find([
					'goods_commonid' => $ginfo['goods_commonid']
				]);
				$obj = [
					$item['receiver_name'],
					$item['phone'],
					$item['idcard'],
					$p['area_name'] . ' ' . $c['area_name'] . ' ' . $a['area_name'],
					$item['address'],
					$g['goods_name'],
					$gminfo['goods_serial'],
					$g['goods_pay_price'],
					$gext['unit'],
					$gext['originCountry'],
					$gext['gcode'],
					$gext['gmodel'],
					$gext['brand'],
					$g['goods_pay_price'] * $g['goods_num'],
					$g['goods_num'],
					$gext['hCount'],
					$gext['hCount2'],
					$item['shipping_code'],
					$item['order_tax'],
					$a['area_code'],
					$gext['ciqGno'],
					$sinfo['ebcName'],
					$sinfo['ebcCode'],
					$item['order_amount'],
					$item['order_sn'],
					$item['api_pay_sn']
				];
				$arr[] = $obj;
			}

			// print_r($rinfo);
		}
			// print_r($arr);
		require_once SIN_ROOT_PATH . '/static/lib/PHPExcel.php';
			require_once SIN_ROOT_PATH . '/static/lib/PHPExcel/IOFactory.php';
			$index = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
			$objPHPExcel = new PHPExcel();                     //实例化一个PHPExcel()对象
			$objSheet = $objPHPExcel->getActiveSheet();        //选取当前的sheet对象
			$objSheet->setTitle('Simple');
			foreach ($arr as $k => $v) {

				foreach ($v as $kk => $vv) {
					$objSheet->getColumnDimension($index[$kk])->setAutoSize(true);

//					$objSheet->setCellValue($index[$kk] . $k ,(string)$vv);
//					$objSheet->getStyle($index[$kk] . $k)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
					$objSheet->setCellValueExplicit($index[$kk] . $k, $vv . ' ', PHPExcel_Cell_DataType::TYPE_STRING);
				}
//				$objPHPExcel->getActiveSheet()->getStyle($k)->getNumberFormat()
//					->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
			}
			//对当前sheet对象命名
//			$objSheet->fromArray($arr);  //利用fromArray()直接一次性填充数据
			$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');   //设定写入excel的类型
			$fileName = uniqid() . '.xlsx';
			$objWriter->save(SIN_PACKAGE_PATH . '/excel_import_export/output_files/' . $fileName);
			echo '/packages/excel_import_export/output_files/' . $fileName;



	}
	/**
	 * 用户中心右边，小导航
	 *
	 * @param string	$menu_type	导航类型
	 * @param string 	$menu_key	当前导航的menu_key
	 * @return
     */
    private function profile_menu($menu_type='',$menu_key='') {
        Language::read('member_layout');
        switch ($menu_type) {
        	case 'list':
            $menu_array = array(
            array('menu_key'=>'store_order',		'menu_name'=>Language::get('nc_member_path_all_order'),	'menu_url'=>'index.php?act=store_order'),
            array('menu_key'=>'state_new',			'menu_name'=>Language::get('nc_member_path_wait_pay'),	'menu_url'=>'index.php?act=store_order&op=index&state_type=state_new'),
            array('menu_key'=>'state_pay',	        'menu_name'=>Language::get('nc_member_path_wait_send'),	'menu_url'=>'index.php?act=store_order&op=store_order&state_type=state_pay'),
            array('menu_key'=>'state_send',		    'menu_name'=>Language::get('nc_member_path_sent'),	    'menu_url'=>'index.php?act=store_order&op=index&state_type=state_send'),
            array('menu_key'=>'state_success',		'menu_name'=>Language::get('nc_member_path_finished'),	'menu_url'=>'index.php?act=store_order&op=index&state_type=state_success'),
            array('menu_key'=>'state_cancel',		'menu_name'=>Language::get('nc_member_path_canceled'),	'menu_url'=>'index.php?act=store_order&op=index&state_type=state_cancel')
            );
			$sinfo = ml('store')->find($_SESSION['store_id']);
			if($sinfo['useIETAPI'] == 1){
				$menu_array[] = array('menu_key'=>'order_list_detail',	'menu_name'=>'详细订单列表',	'menu_url'=>'index.php?act=store_order&op=index_detail&state_type=order_list_detail');
				$menu_array[] = array('menu_key'=>'imported_order',		'menu_name'=>'外部订单',	'menu_url'=>'index.php?act=store_order&op=index&state_type=imported_order');
				$menu_array[] = array('menu_key'=>'imported_order_list',		'menu_name'=>'导入列表',	'menu_url'=>'index.php?act=store_order&op=imported_order_list');
				$menu_array[] = array('menu_key'=>'tonglian_list',		'menu_name'=>'通联预付卡',	'menu_url'=>'index.php?act=store_order&op=tonglian_list');
			}
            break;
        }
        Tpl::output('member_menu',$menu_array);
        Tpl::output('menu_key',$menu_key);
    }


    public function sellerConfirmOp(){
//        require_once __DIR__ . "/payment.php";
//
//        $_GET["noreturn"] = 1;
//        $_GET["payment_code"] = "manual";
//        $_POST['extra_common_param'] = "real_order";
//        $oinfo = Model()->table("order")->where([
//            "order_id" => $_GET["id"],
//            "store_id" => $_SESSION["store_id"]
//        ])->field("pay_sn")->find();
//        if (is_null($oinfo)){
//            return "失败";
//        }
//        $_POST['out_trade_no'] = $oinfo["pay_sn"];
//        $paymentControl = new paymentControl();
//        $paymentControl->notifyOp();

        Model()->table("order")->where([
            "order_id" => $_GET['id'],
            "store_id" => $_SESSION["store_id"],
            "qk_seller_confirm" => 0
        ])->update([
            "qk_seller_confirm" => 1
//            "payment_code" => "manual"
        ]);
        echo "ok";
    }
}
